package com.dingzhen.support;

import org.elasticsearch.script.Script;

import java.util.HashMap;
import java.util.Map;

public class ScriptBuilder {
    private final StringBuilder scriptPlainText;
    private final Map<String, Object> fields;
    private ScriptBuilder() {
        scriptPlainText = new StringBuilder();
        fields = new HashMap<>();
    }

    public static ScriptBuilder builder(){
        return new ScriptBuilder();
    }
    public ScriptBuilder field(String name, Object value) {
        this.fields.put(name,value);
        return this;
    }

    public ScriptBuilder fields(Map<String, Object> fields) {
        this.fields.putAll(fields);
        return this;
    }

    public Script build() {
        fields.forEach((k,v)-> this.scriptPlainText.append("ctx._source['").append(k).append("']=").append(v).append(";"));
        return new Script(scriptPlainText.toString());
    }
}
